package _07_串;

import java.util.HashSet;
import java.util.Set;

public class _2063_所有子字符串中的元音 {

    public static void main(String[] args) {

        _2063_所有子字符串中的元音 v = new _2063_所有子字符串中的元音();
        String word = "aba";
        System.out.println(v.countVowels(word));

    }

    private final Set<Character> set = new HashSet<>();
    {

        set.add('a');
        set.add('e');
        set.add('i');
        set.add('o');
        set.add('u');
    }

    public long countVowels(String word) {
        // 前i项的有元音字母
        int len = word.length();
        // 前i项的元音字母和
        long[] dp = new long[len + 1];
        // 元音总和
        long total = 0;
        for (int i = 1; i <= len; i++) {
            int vowel = set.contains(word.charAt(i - 1))? 1: 0;
            dp[i] = dp[i - 1] + vowel;
            for (int j = i - 1; j >= 0; j--) {
                total += dp[i] - dp[j];
            }
        }
        return total;
    }

    // 对偶性质
    public long countVowels1(String word) {
        char[] raw = {'a', 'e', 'i', 'o', 'u'};
        boolean[] map = new boolean[26];
        for (char c : raw) {
            map[c - 'a'] = true;
        }
        int len = word.length();
        long ans = 0;
        for (int i = 0; i < len; i++) {
            if (map[word.charAt(i) - 'a']) {
                ans += (long) (i + 1) * (len - i);
            }
        }
        return ans;
    }

}
